/*
 * Copyright 2007 Xplayat, S.L. All rights reserved.
 * Propiedad de Xplayat. 
 */
package facelets.component;

import javax.faces.component.UIOutput;

import org.apache.myfaces.component.html.ext.HtmlPanelGrid;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.MetaRuleset;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.jsf.ComponentConfig;
import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;

public class PanelBlockComponentHandler extends HtmlComponentHandler { // NO_UCD
	
	private final TagAttribute title;
		
	public PanelBlockComponentHandler(ComponentConfig tagConfig) {
		super(tagConfig);
		title = getAttribute("label");
	}
	
	
	/* (non-Javadoc)
	 * @see com.sun.facelets.tag.MetaTagHandler#setAttributes(com.sun.facelets.FaceletContext, java.lang.Object)
	 */
	public void setAttributes(FaceletContext ctx, Object instance) {
		
		HtmlPanelGrid panel = (HtmlPanelGrid)instance;
		if ( getAttribute("styleClass") == null )
			panel.setStyleClass("block");
		//if ( getAttribute("frame") == null )
			//panel.setFrame("border");
		//if ( getAttribute("headerClass") == null )
		//	panel.setHeaderClass("blockheader");
		if ( getAttribute("columns") == null )
			panel.setColumns(6);
		
		if ( title!=null && title.getValue()!=null) {
			UIOutput f = (UIOutput)ctx.getFacesContext().getApplication().createComponent(UIOutput.COMPONENT_TYPE);
			String label = title.getValue();
			
			if (!title.isLiteral())
//				f.setValueBinding("value", ctx.getFacesContext().getApplication().createValueBinding(title.getValue()));
				f.setValueExpression("value", title.getValueExpression(ctx, String.class));
			else f.setValue(label);
		
			((HtmlPanelGrid) instance).getFacets().put("header", f);
		}
		
		super.setAttributes(ctx, instance);
	}
	
	protected MetaRuleset createMetaRuleset(@SuppressWarnings("rawtypes") Class type) {
		return super.createMetaRuleset(type).ignore("label");
    }
}
